package luke4fun.wheresmymoney.adapter.list.impl;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import luke4fun.wheresmymoney.R;
import luke4fun.wheresmymoney.adapter.list.BasicAdapter;
import luke4fun.wheresmymoney.bean.SummaryBean;
import luke4fun.wheresmymoney.viewholder.LegendGridViewHolder;


public class StatisticLegendAdapter extends BasicAdapter<SummaryBean> {
	
	private List<SummaryBean> dataList;
	private LayoutInflater inflater;

	public StatisticLegendAdapter(Context context) {
		this.inflater = LayoutInflater.from(context);
		dataList = new ArrayList<SummaryBean>();
	}
	
	@Override
	public int getCount() {
		return dataList == null ? 0 : dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LegendGridViewHolder holder;
		
		// set the view holder for reuse
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.grid_legend, null);
			holder = new LegendGridViewHolder();
			holder.colorSymbol = (View) convertView.findViewById(R.id.color_symbol);
			holder.colorDesc = (TextView) convertView.findViewById(R.id.color_desc);
			holder.percentage = (TextView) convertView.findViewById(R.id.percentage);
			
			convertView.setTag(holder);
		} else {
			holder = (LegendGridViewHolder) convertView.getTag();
		}
		
		SummaryBean bean = dataList.get(position);
		holder.colorSymbol.setBackgroundColor(bean.getColor());
		holder.colorDesc.setText(bean.getCategory() + ": ");
		
		DecimalFormat format = (DecimalFormat) NumberFormat.getInstance();
		format.applyPattern("0.00");
		holder.percentage.setText(format.format(bean.getPercentage()) + "%");
		
		return convertView;
	}

	public void updateList(List<SummaryBean> list) {
		this.dataList = list;
		notifyDataSetChanged();
	}
}
